package com.csust.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ReflectionAccessMethod {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("com.csust.reflection.Employee");
        Constructor<?> constructor = clazz.getConstructor(String.class, int.class, double.class);
        Object o = constructor.newInstance("Sail", 22, 25000.00);
        // public 的mehtod 的调用
        Method sayHello = clazz.getDeclaredMethod("sayHello");
        sayHello.invoke(o);
        //非public 的method 的调用

        Method stateSalary = clazz.getDeclaredMethod("stateSalary");
        stateSalary.setAccessible(true);
        stateSalary.invoke(o);


    }
}
class Employee{
    public String name;
    public  int age;
    private double salary;

    public Employee() {
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void sayHello(){
        System.out.println("Hello,I am "+name + "My age is " + age);
    }
    private void stateSalary(){
        System.out.println("My salary is "+salary);
    }


}

